package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public BusinessDataVO businessData(LocalDateTime begin,LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        map.put("status", Orders.COMPLETED);
        //总营业额
        Double turnover = orderMapper.sumByMap(map);
        if(turnover==null){
            turnover= (double) 0;
        }
        //有效订单
        Integer validOrderCount = orderMapper.CountByMap(map);
        //当前所有订单
        map.remove("status");
        map.put("begin", begin);
        map.put("end", end);
        int total = orderMapper.CountByMap(map);
        //新增用户
        Long newUsers = orderMapper.UserSumByMap(map);

        double unitPrice;
        double orderCompletionRate;
        if(total==0||validOrderCount==0){
             unitPrice=  0;
             orderCompletionRate=0;
        }else {
            unitPrice=turnover / validOrderCount;
            orderCompletionRate=validOrderCount * (1.00) / total;
        }


        return BusinessDataVO.builder()
                .newUsers(Math.toIntExact(newUsers))
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .unitPrice(unitPrice)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        //查询已停售套餐数量
        //查询已起售套餐数量
        Integer discontinued = setmealMapper.getNumByStatus(StatusConstant.DISABLE);
        Integer sold = setmealMapper.getNumByStatus(StatusConstant.ENABLE);

        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public DishOverViewVO overviesDishes() {
        //查询已停售菜品数量
        //查询已起售菜品数量
        Integer discontinued = dishMapper.getNumByStatus(StatusConstant.DISABLE);
        Integer sold = dishMapper.getNumByStatus(StatusConstant.ENABLE);

        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        Integer allOrders = orderMapper.getByStatus(null);
        Integer cancelledOrders = orderMapper.getByStatus(Orders.CANCELLED);
        Integer completedOrders = orderMapper.getByStatus(Orders.COMPLETED);
        Integer deliveredOrders = orderMapper.getByStatus(Orders.CONFIRMED);
        Integer waitingOrders = orderMapper.getByStatus(Orders.TO_BE_CONFIRMED);

        return OrderOverViewVO.builder()
                .deliveredOrders(deliveredOrders)
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
